<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="ZhangPing">





<title>Intent隐式调用-启动其它应用程序 | ZhangPing&#39;s Blog</title>



    <link rel="icon" href="/favicon.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/js/script.js"></script>
    
    <script src="/js/tocbot.min.js"></script>
    



    
    
        
    


<meta name="generator" content="Hexo 4.2.0"></head>
<body>
    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/">ZhangPing&#39;s Blog</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/">ZhangPing&#39;s Blog</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
        <div class="main">
            <div class="container">
    
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">Intent隐式调用-启动其它应用程序</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/">ZhangPing</a>
                    

                    
                        <span class="post-time">
                        Date: <a href="#">October 21, 2016&nbsp;&nbsp;22:00:00</a>
                        </span>
                    
                    
                        <span class="post-category">
                    Category:
                            
                                <a href="/categories/Android-%E7%9F%A5%E8%AF%86%E7%82%B9%E6%95%B4%E7%90%86/">Android-知识点整理</a>
                            
                        </span>
                    
                </div>
            
        </header>

        <div class="post-content">
            <p>Intent的两种基本用法：</p>
<ul>
<li><p>显式的Intent，即在构造Intent对象时就指定接收者，这种方式与普通的函数调用类似；</p>
</li>
<li><p>隐式的Intent，即Intent的发送者在构造Intent对象时，并不知道也不关心接收者是谁，有利于降低发送者和接收者之间的耦合。</p>
</li>
</ul>
<p>显示调用 ：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Intent intent = <span class="keyword">new</span> Intent();</span><br><span class="line">intent.setClass(A.<span class="keyword">this</span>,B<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line">intent.setClassName(<span class="string">"com.android.activity"</span>,B<span class="class">.<span class="keyword">class</span>)</span>;<span class="comment">//指定包名下的类名 </span></span><br><span class="line">startActivity(intent);</span><br></pre></td></tr></table></figure>

<p>隐式调用：</p>
<p>1.首先在应用A的某个类中：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Intent intent = <span class="keyword">new</span> Intent();</span><br><span class="line">intent.setAction(<span class="string">"com.android.activity"</span>);<span class="comment">//"com.android.activity"为自定义action</span></span><br><span class="line"><span class="comment">//Intent intent = new Intent("com.android.activity");或者使用这种写法</span></span><br><span class="line">intent.putExtra(<span class="string">"KEY"</span>,<span class="string">"VALUE"</span>);<span class="comment">//传递数据</span></span><br><span class="line">startActivity(intent);</span><br></pre></td></tr></table></figure>

<p>2.在应用B中接收A发来的action：</p>
<p>如果DemoActivity是程序的入口：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;activity android:name&#x3D;&quot;.DemoActivity&quot;&gt;</span><br><span class="line">	&lt;intent-filter&gt;</span><br><span class="line">		&lt;action android:name&#x3D;&quot;android.intent.action.MAIN&quot;</span><br><span class="line">		&lt;action android:name&#x3D;&quot;com.android.activity&quot; &#x2F;&gt;</span><br><span class="line">		&lt;category android:name&#x3D;&quot;android.intent.category.LAUNCHER&quot; &#x2F;&gt;</span><br><span class="line">	&lt;&#x2F;intent-filter&gt;</span><br><span class="line">&lt;&#x2F;activity&gt;</span><br></pre></td></tr></table></figure>
<p>如果DemoActivity不是程序的入口：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;activity android:name&#x3D;&quot;.DemoActivity&quot;&gt;</span><br><span class="line">	&lt;intent-filter&gt;</span><br><span class="line">		&lt;action android:name&#x3D;&quot;com.android.activity&quot; &#x2F;&gt;</span><br><span class="line">		&lt;category android:name&#x3D;&quot;android.intent.category.DEFAULT&quot; &#x2F;&gt;&lt;!--必须加，不要遗漏--&gt;</span><br><span class="line">	&lt;&#x2F;intent-filter&gt;</span><br><span class="line">&lt;&#x2F;activity&gt;</span><br></pre></td></tr></table></figure>

<p>以上简单的介绍了从一个程序使用隐式意图打开另一个应用activity的用法。这种用法在只有一个app的情况下应用较少，主要应用于存在有相互调起的逻辑的应用之间。与显示意图相同，同样可以通过putExtra()方法传递数据。</p>

        </div>

        
            <section class="post-copyright">
                
                    <p class="copyright-item">
                        <span>Author:</span>
                        <span>ZhangPing</span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>Permalink:</span>
                        <span><a href="https://zhangpingv5.gitee.io/2016/10/21/Intent%E9%9A%90%E5%BC%8F%E8%B0%83%E7%94%A8-%E5%90%AF%E5%8A%A8%E5%85%B6%E5%AE%83%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F/">https://zhangpingv5.gitee.io/2016/10/21/Intent%E9%9A%90%E5%BC%8F%E8%B0%83%E7%94%A8-%E5%90%AF%E5%8A%A8%E5%85%B6%E5%AE%83%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F/</a></span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>License:</span>
                        <span>转载请注明出处</span>
                    </p>
                
                
                     <p class="copyright-item">
                         <span>Slogan:</span>
                         <span>Self-descipline grants me freedom.</span>
                     </p>
                

            </section>
        
        <section class="post-tags">
            <div>
                <span>Tag(s):</span>
                <span class="tag">
                    
                    
                        <a href="/tags/Intent/"># Intent</a>
                    
                        
                </span>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/2016/11/22/Volley%20io%E8%AF%BB%E5%8F%96%E7%BC%93%E5%AD%98%E9%83%A8%E5%88%86%E4%B8%AD%E6%96%87%E4%B9%B1%E7%A0%81%E9%97%AE%E9%A2%98/">Volley io读取缓存部分中文乱码问题</a>
            
            
            <a class="next" rel="next" href="/2016/06/21/%E8%87%AA%E5%AE%9A%E4%B9%89Lint/">自定义Lint</a>
            
        </section>


    </article>
</div>

        </div>
        <footer id="footer" class="footer">
    <div class="copyright">
        <span>© ZhangPing | Powered by <a href="https://hexo.io" target="_blank">Hexo</a> & <a href="https://github.com/Siricee/hexo-theme-Chic" target="_blank">Chic</a></span>
    </div>
</footer>

    </div>
</body>
</html>
